﻿#pragma execution_character_set("utf-8")
#include "httpservice.h"
#include "requestmapper.h"

#include <QCoreApplication>
#include <QSettings>

HttpService::HttpService(QObject *parent)
    : QObject(parent)
{
    open();
}

HttpService::~HttpService()
{
    close();
}

void HttpService::open()
{
    QString configFileName = QCoreApplication::applicationDirPath() + "/config/WebConfig.ini";
    // Configure logging into a file
    //    QSettings *logSettings = new QSettings(configFileName, QSettings::IniFormat, this);
    //    logSettings->beginGroup("logging");
    //    auto logger = new FileLogger(logSettings, 10000, this);
    //#ifndef QT_DEBUG
    //    logger->installMsgHandler();
    //#endif

    // Configure session store
    QSettings *sessionSettings = new QSettings(configFileName, QSettings::IniFormat, this);
    sessionSettings->beginGroup("sessions");
    httpSessionStore = new HttpSessionStore(sessionSettings, this);

    // Configure static file controller
    //    QSettings *fileSettings = new QSettings(configFileName, QSettings::IniFormat, this);
    //    fileSettings->beginGroup("docroot");
    //    s_staticFileController = new StaticFileController(fileSettings, this);

    // Configure and start the TCP listener
    QSettings *listenerSettings = new QSettings(configFileName, QSettings::IniFormat, this);
    listenerSettings->beginGroup("listener");
    httpListener = new HttpListener(listenerSettings, new RequestMapper(this), this);
}

void HttpService::close()
{
    delete httpListener;
    delete httpSessionStore;
}
